package com.xgz.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.github.pagehelper.PageInfo;
import com.xgz.dao.TUserDao;
import com.xgz.entity.LayData;
import com.xgz.entity.MyPage;
import com.xgz.entity.TUser;
import com.xgz.service.TUserService;
import com.xgz.util.ConfigUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * (TUser)表控制层
 *
 * @author makejava
 * @since 2022-10-19 19:31:19
 */
@RestController
public class TUserController {
    /**
     * 服务对象
     */
    @Autowired
    private TUserService tUserService;
    @Autowired
    private TUserDao tUserDao;

    /**
     * 添加用户
     * @param tUser
     * @return
     */
    @RequestMapping("adduser")
    public LayData addUser(TUser tUser){
        System.out.println("前台传过来的参数是:"+tUser);
        boolean b = tUserService.addUser(tUser);
        return new LayData(b);
    }

    /**
     *登录
     * @param session
     * @param tUser
     * @return
     */
    @RequestMapping("login")
    public LayData login(HttpSession session,TUser tUser){
        LayData layData = new LayData();
        TUser login = tUserService.login(tUser);
        if (login!=null) {
            layData.setData(true);
            //查询数据库里是否有数据
            List<TUser> login1 = tUserDao.login(tUser.getUsername());
            session.setAttribute("user", login);
            session.setAttribute("imgpath", login1.get(0).getImgpath());
            session.setAttribute("login1", login1.get(0).getId());
            System.out.println("login1 = " + login1.get(0).getId());
        }
        return layData;
    }

    /**
     * 查询所有
     * @param myPage
     * @param tUser
     * @return
     */
    @RequestMapping("queryAll")
    public LayData queryAll(MyPage myPage,TUser tUser){
        PageInfo pageInfo = tUserService.queryAll(myPage, tUser);
        return new LayData(pageInfo);
    }

    /**
     * 模糊查询
     * @param tUser
     * @return
     */
    @RequestMapping("querylike")
    public LayData querylike(TUser tUser){
        PageInfo pageInfo = tUserService.query(tUser);
        System.out.println("pageInfo = " + pageInfo.toString());
        LayData layData = new LayData();
        layData.setCode(0);
        layData.setData(pageInfo);
        return layData;
    }
    /**
     * 修改
     * @param tUser
     * @return
     */
    @RequestMapping("updUser")
    public LayData updUser(TUser tUser){
        boolean b = tUserService.updUser(tUser);
        return new LayData(b);
    }

    /**
     * 用户名失焦
     * @param username
     * @return
     */
    @RequestMapping("queryname/{username}")
    public LayData queryUserByName(@PathVariable String username){
        boolean b = tUserService.queryByName(username);
        System.out.println("b = " + b);
        return new LayData(b);
    }


    /**
     * 上传文件到阿里云
     * @param file
     * @param request
     * @return
     */
    @PostMapping("uploadLayuiFile")
    public LayData uploadFile4(MultipartFile file, HttpServletRequest request){
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = ConfigUtil.END_POINT;
        // oss-cn-beijing.aliyuncs.com
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = ConfigUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConfigUtil.ACCESS_KEY_SECRET;
        // 填写Bucket名称，例如examplebucket。
        String bucketName = ConfigUtil.BUCKET_NAME;
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        // 新的名字
        String s = UUID.randomUUID().toString();// DJKAHGFJSHGHKFSHKJDFG
        // 获取原来的文件的后缀 oldname
        String oldname=file.getOriginalFilename();
        int i = oldname.lastIndexOf(".");
        String suffixname = oldname.substring(i);
        //System.out.println("suffixname = " + suffixname);
        String newname=s+suffixname;
        //System.out.println("newname = " + newname);
        String objectName = newname;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            // 填写字符串。
            String content = "Hello OSS";
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file.getInputStream());
            // 上传字符串。
            ossClient.putObject(putObjectRequest);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        String picpath="https://"+bucketName+"."+endpoint+"/"+objectName;
        return new LayData(picpath);
    }




}

